package com.letoken.platform.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.letoken.platform.app.service.LetokenUserAuthService;
import com.letoken.platform.pub.annotation.auth.NotAuthorization;
import com.letoken.platform.pub.annotation.log.SysLog;
import com.letoken.platform.pub.enums.LogOperationModule;
import com.letoken.platform.pub.enums.LogOperationType;
import com.letoken.platform.pub.res.ApiResponse;
import com.letoken.platform.pub.util.LocalEnvUtils;
import com.letoken.platform.pub.util.RequestUtil;
import com.letoken.platform.repo.app.req.user.AppUserLoginReq;
import com.letoken.platform.repo.app.res.user.AppUserLoginRes;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author letokenlabs
 * @date 2022/11/14$
 */
@RestController
@RequestMapping("/user")
@Api(tags = "App用户授权接口")
@Slf4j
public class LetokenUserAuthController {

    @Autowired
    private LetokenUserAuthService letokenUserAuthService;

    @Autowired
    private HttpServletRequest request;


    @NotAuthorization
    @SysLog(description = "用户登录", operationType = LogOperationType.UNKNOWN, operationModule = LogOperationModule.UNKNOWN,
            coreFields = {"login"})
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    public ApiResponse<AppUserLoginRes> login(@RequestBody @Validated AppUserLoginReq req) {
        log.info(JSONObject.toJSONString(req));
        return ApiResponse.create(letokenUserAuthService.login(req, RequestUtil.getIpAddr(request)));
    }



//    @NotAuthorization
//    @PostMapping("/loginDev")
//    @ApiOperation(value = "用户登录（dev）", notes = "用户登录(dev)")
//    public ApiResponse<AppUserLoginRes> loginDev(@RequestBody @Validated AppUserLoginReq req) {
//        if (LocalEnvUtils.isDev()){
//            return ApiResponse.create(letokenUserAuthService.loginDev(req, RequestUtil.getIpAddr(request)));
//        }
//        return ApiResponse.create();
//    }

}
